package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.CollectCommandInst;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/24 12:22
 * @description
 */
@Repository("collectCommandInstArchiverDao")
public class CollectCommandInstArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `CMD_INST_ID`, `V_CMD_INST_ID`, `NCT_CODE`, `PARAM_LIST`, `DIRECTION`, `V_CMD_CODE`, `PRIORITY`, `CREATE_TIME`, `FINISH_TIME`, `STATUS`, `SEND_TIME`, `CMD_SEND_LOG`, `RECV_TIME`, `CMD_RECV_LOG`, `PARTITION_CODE`, `REDIS_CLUSTER_CODE`, `collector_Inst_Id`");
        sql.append(" FROM COLLECT_COMMAND_INST WHERE V_CMD_INST_ID = ?");
        List<CollectCommandInst> list = jdbcTemplate.query(sql.toString(), new Object[]{o} , new BeanPropertyRowMapper<>(CollectCommandInst.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `CMD_INST_ID`, `V_CMD_INST_ID`, `NCT_CODE`, `PARAM_LIST`, `DIRECTION`, `V_CMD_CODE`, `PRIORITY`, `CREATE_TIME`, `FINISH_TIME`, `STATUS`, `SEND_TIME`, `CMD_SEND_LOG`, `RECV_TIME`, `CMD_RECV_LOG`, `PARTITION_CODE`, `REDIS_CLUSTER_CODE`, `collector_Inst_Id`");
        sql.append(" FROM COLLECT_COMMAND_INST WHERE V_CMD_INST_ID in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<CollectCommandInst> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                CollectCommandInst collectCommandInst = new CollectCommandInst();
                collectCommandInst.setCmdInstId(resultSet.getLong("CMD_INST_ID"));
                collectCommandInst.setvCmdInstId(resultSet.getString("V_CMD_INST_ID"));
                collectCommandInst.setNctCode(resultSet.getString("NCT_CODE"));
                collectCommandInst.setParamList(resultSet.getString("PARAM_LIST"));
                collectCommandInst.setDirection(resultSet.getLong("DIRECTION"));
                collectCommandInst.setvCmdCode(resultSet.getString("V_CMD_CODE"));
                collectCommandInst.setPriority(resultSet.getLong("PRIORITY"));
                collectCommandInst.setCreateTime(resultSet.getString("CREATE_TIME"));
                collectCommandInst.setFinishTime(resultSet.getString("FINISH_TIME"));
                collectCommandInst.setStatus(resultSet.getString("STATUS"));
                collectCommandInst.setSendTime(resultSet.getString("SEND_TIME"));
                collectCommandInst.setCmdSendLog(resultSet.getString("CMD_SEND_LOG"));
                collectCommandInst.setRecvTime(resultSet.getString("RECV_TIME"));
                collectCommandInst.setCmdRecvLog(resultSet.getString("CMD_RECV_LOG"));
                collectCommandInst.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                collectCommandInst.setRedisClusterCode(resultSet.getString("REDIS_CLUSTER_CODE"));
                collectCommandInst.setCollectorInstId(resultSet.getString("COLLECTOR_INST_ID"));
                return collectCommandInst;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `collect_command_inst`");
        sql.append(" (`CMD_INST_ID`, `V_CMD_INST_ID`, `NCT_CODE`, `PARAM_LIST`, `DIRECTION`, `V_CMD_CODE`, `PRIORITY`, `CREATE_TIME`, `FINISH_TIME`, `STATUS`, `SEND_TIME`, `CMD_SEND_LOG`, `RECV_TIME`, `CMD_RECV_LOG`, `PARTITION_CODE`, `ARCHIVER_TIME`, `REDIS_CLUSTER_CODE`, `collector_Inst_Id`)");
        sql.append(" VALUES (:cmdInstId,:vCmdInstId,:nctCode,:paramList,:direction,:vCmdCode,:priority,:createTime,:finishTime,:status,:sendTime,:cmdSendLog,:recvTime,:cmdRecvLog,:partitionCode,:archiverTime,:redisClusterCode,:collectorInstId)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {

    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM COLLECT_COMMAND_INST WHERE CMD_INST_ID = :cmdInstId");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
